//
//  StoresManageViewController.m
//  SameWorld(merchant)
//
//  Created by 李小朋 on 2017/3/30.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "StoresManageViewController.h"
#import "StoresManageCell.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"
#import "StoresUserEditorViewController.h"
#import "StoresCodeView.h"
#import "UserInfo.h"
#import <UIImageView+WebCache.h>
@interface StoresManageViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) UserInfo *userInfo;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSArray *titles;
@property (nonatomic ,strong) NSArray *images;
@property (nonatomic ,strong) UIImageView *headerBackView;
@property (nonatomic ,strong) UIView *tableViewHeaderView;
@property (nonatomic ,assign) NSInteger imageHeight;
@property (nonatomic ,strong) UILabel *describeLab;
@property (nonatomic ,strong) NSArray *dataSource;
@end

@implementation StoresManageViewController

- (NSArray *)titles {
    if (!_titles) {
        _titles = @[@[@"联系人：",@"电    话：",@"地    址：",@"商    圈："],@[@"收款方：",@"账    号：",@"收款人：",@"分    类：",@"创建时间："]];
    }
    return _titles;
}

- (NSArray *)images {
    if (!_images) {
        _images = @[@[@"lianxiren",@"dianhua",@"dizhi",@"shangquan"],@[@"shoukuanfangshi",@"shoukuanzhanghao",@"shoukuanren",@"fenlei",@"chuangjianshijian"]];
    }
    return _images;
}

- (NSArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource =@[@[_userInfo.contacts,_userInfo.phone,_userInfo.address,_userInfo.tradingarea],@[_userInfo.shopmc,_userInfo.account_unmber,_userInfo.payee,[NSString stringWithFormat:@"%@-%@",_userInfo.one_classification,_userInfo.two_classification],_userInfo.sj]];
    }
    return _dataSource;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //进入当前页面使导航变透明
    self.navigationController.navigationBar.translucent=YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _userInfo = [UserInfo sharedUserInfo];
    
    [self createTableViewUI];
    [self setNarBarUI];
    
}

- (void)setNarBarUI {

    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftItemClicked:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"erweima"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightItemClicked:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

#pragma mark 返回按钮
- (void)leftItemClicked:(UIBarButtonItem *)sender {
    self.navigationController.navigationBar.translucent=NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 二维码按钮
- (void)rightItemClicked:(UIBarButtonItem *)sender {
     // 二维码
    StoresCodeView *codeView= [[StoresCodeView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [codeView showCustomStoresCodeView];
}

- (void)createTableViewUI {
    
    _imageHeight = 200;//背景图片的高度
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    [self.view addSubview:self.tableView];
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"StoresManageCell" bundle:nil] forCellReuseIdentifier:@"StoresManageCell"];
    
    [self createTableViewHeaderView];
}

- (void)createTableViewHeaderView {

    _tableViewHeaderView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, size_width, _imageHeight))];
    _headerBackView = [[UIImageView alloc] init];
    
    //背景图
    _headerBackView.frame = CGRectMake(0, 0, size_width, _imageHeight);
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",url_Base,_userInfo.top_img];
    [_headerBackView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"占位图-1"]];
    _headerBackView.backgroundColor = [UIColor redColor];
    [_tableViewHeaderView addSubview:_headerBackView];
    
    //渐变背景图
    UIImageView *gradientImage = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, size_width, _imageHeight))];
    gradientImage.image = [UIImage imageNamed:@"门店管理——透明渐变"];
    [_tableViewHeaderView addSubview:gradientImage];
    
    
    UIImageView *shopImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dian"]];
    shopImage.frame = CGRectMake(8, self.headerBackView.frame.size.height - 60, 18, 18);
    [self.tableViewHeaderView addSubview:shopImage];
    
    // 店铺名称
    UILabel *nameLab = [[UILabel alloc] initWithFrame:(CGRectMake(36, self.headerBackView.frame.size.height - 62, 200, 22))];
    nameLab.text = [NSString stringWithFormat:@"%@",_userInfo.shopmc];
    nameLab.textColor = [UIColor whiteColor];
    nameLab.layer.shadowOpacity = 0.8;
    nameLab.layer.shadowColor = [UIColor blackColor].CGColor;
    nameLab.layer.shadowOffset = CGSizeMake(0.5, 1.5);
    [self.tableViewHeaderView addSubview:nameLab];
    
    // 店铺内容描述
    UILabel *describeLab = [UILabel setLabelWithText:[NSString stringWithFormat:@"%@",_userInfo.describe] fontSize:10 color:[UIColor whiteColor]];
    [UILabel changeWordSpaceForLabel:describeLab WithSpace:1.4];
    describeLab.frame = CGRectMake(36, nameLab.frame.size.height + nameLab.frame.origin.y + 6, size_width - 36 - 33, 12);
    describeLab.numberOfLines = 0;
    describeLab.lineBreakMode = NSLineBreakByWordWrapping;
    [self.tableViewHeaderView addSubview:describeLab];
    [describeLab sizeToFit];
    self.describeLab = describeLab;
    
    self.tableView.tableHeaderView = _tableViewHeaderView;
    
    UIButton *foldBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    foldBtn.frame = CGRectMake(size_width - 18, describeLab.frame.origin.y, 10, 10);
    [foldBtn setImage:[UIImage imageNamed:@"xiale"] forState:(UIControlStateNormal)];
     [foldBtn setImage:[UIImage imageNamed:@"shangle"] forState:(UIControlStateSelected)];
    foldBtn.selected = YES;
    [foldBtn addTarget:self action:@selector(foldBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.tableViewHeaderView addSubview:foldBtn];
}

- (void)foldBtnClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.describeLab.numberOfLines = 0;
        self.describeLab.lineBreakMode = NSLineBreakByWordWrapping;
    }else{
        self.describeLab.numberOfLines = 1;
        self.describeLab.lineBreakMode = NSLineBreakByTruncatingTail;
    }
}


#pragma mark - UITableVIew的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titles.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.titles[section] count];;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    StoresManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StoresManageCell" forIndexPath:indexPath];
    
    cell.image.image = [UIImage imageNamed:self.images[indexPath.section][indexPath.row]];
    cell.title.text = self.titles[indexPath.section][indexPath.row];
    cell.title.textColor = color_textOne;
    cell.contentLabel.text = self.dataSource[indexPath.section][indexPath.row];
    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2) {
            cell.modifyImage.image = [UIImage imageNamed:@"xiugai"];
        }else if(indexPath.row == 3){
            cell.lineView.hidden = YES;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 4) {
            cell.lineView.hidden = YES;
        }
    }
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 8;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2) {
            /*
            StoresUserEditorViewController *userVc = [[StoresUserEditorViewController alloc] init];
            NSString *name = self.titles[indexPath.section][indexPath.row];
            NSString *image = self.images[indexPath.section][indexPath.row];
            userVc.userTitle = name;
            userVc.userImage = image;
            [self.navigationController pushViewController:userVc animated:YES];
             */
            [self showAlertWith:@"该功能尚未开放！"];
        }
    }
    
}


#pragma mark - UIScrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 设置顶部图片下拉放大
    CGFloat width = self.view.frame.size.width; // 图片宽度
    CGFloat yOffset = scrollView.contentOffset.y;  // 偏移的y值
    if (yOffset < 0) {
        CGFloat totalOffset = self.imageHeight + ABS(yOffset);
        CGFloat f = totalOffset / self.imageHeight;
        self.headerBackView.frame =  CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset); //拉伸后的图片的frame应该是同比例缩放。
        }
    
    //UITableview处理section的不悬浮
    CGFloat sectionHeaderHeight = 8;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



@end
